<?php 
class User extends AppModel
{
    var $name = 'User';
    
    var $validate = array(
        'username'=>array(
            'style'=>array(
                #this regex need to be revised for special test cases
                'rule'=>array(
                    'custom', '/^[a-z][a-z0-9\._]+$/'
                ), 'required'=>true, 'message'=>'The user name may only contain a-z , dot and underscore'
            ), 'Length'=>array(
                'rule'=>array(
                    'between', 5, 32
                ), 'message'=>'invalid user name length '
            )
        ), 'password'=>array(
            'rule'=>array(
                'between', '6', '32'
            ), 'message'=>'the password length must be 6 characters or more'
        ), 'first_name'=>array(
            'style'=>array(
                'rule'=>array(
                    'custom', '/^[a-zA-Z][a-zA-Z0-9\._]+$/'
                ), 'required'=>true, 'message'=>'The user name may only contain a-z , A-Z , dot ,underscore and hyphen'
            ), 'Length'=>array(
                'rule'=>array(
                    'between', 4, 32
                ), 'message'=>'invalid user name length '
            )
        ), 'middle_name'=>array(
            'style'=>array(
                'rule'=>array(
                    'custom', '/^[a-zA-Z][a-zA-Z0-9\._]+$/'
                ), 'required'=>true, 'message'=>'The user name may only contain a-z , A-Z , dot ,underscore and hyphen'
            ), 'Length'=>array(
                'rule'=>array(
                    'between', 4, 32
                ), 'message'=>'invalid user name length '
            )
        ), 'last_name'=>array(
            'style'=>array(
                'rule'=>array(
                    'custom', '/^[a-zA-Z][a-zA-Z0-9\._]+$/'
                ), 'required'=>true, 'message'=>'The user name may only contain a-z , A-Z , dot ,underscore and hyphen'
            ), 'Length'=>array(
                'rule'=>array(
                    'between', 4, 32
                ), 'message'=>'invalid user name length '
            )
        ), 'birthdate'=>array(
            'rule'=>'validate_birth_date', 'required'=>true
        ), 'user_type_id'=>array(
            'numeric'
        ), 'email'=>array(
            'rule'=>'email', 'required'=>false, 'message'=>'invalid email'
        ),
        'phone_number'=>array('rule'=>array('custom', '/^[0-9]{10}$/'),
                              'required'=>true,
                              'message'=>'invalid phone number'
        ),
        #'profile_picture_url' => array('required'=>false),
        'academic_year'=>array(
            'notempty'
        ), 'acm_member'=>array(
            'numeric'
        ), 'custom_link_url'=>array(
            'rule'=>'url', 'required'=>false, 'message'=>'invalid url'
        )
    );
    
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'UserType'=>array(
            'className'=>'UserType', 
            'foreignKey'=>'user_type_id', 
            'conditions'=>'', 
            'fields'=>'', 
            'order'=>''
        )
    );
    
    var $hasMany = array(
        'EventInstructor'=>array(
            'className'=>'EventInstructor', 
            'foreignKey'=>'user_id', 
            'dependent'=>false, 
            'conditions'=>'', 
            'fields'=>'', 
            'order'=>'', 
            'limit'=>'', 
            'offset'=>'', 
            'exclusive'=>'', 
            'finderQuery'=>'', 
            'counterQuery'=>''
        ),
        'EventAttendee' => array(
        	'className' => 'EventAttendee',
        	'foreignKey' => 'user_id',
        	'dependent' => true
        )
    );
    
    #takes a user name and password and check if the user is already registered or not
    function validateLogin($data)
    {
        
        $user = $this->find(array(
            'username'=>$data['username'], 'password'=>md5($data['password'])
        ), array(
            'id', 'username', 'first_name', 'last_name', 'user_type_id', 'email','activation_key'
        ));
        //found a user and is already activated
        if (empty($user) == false && $user['User']['activation_key'] == '')
            return $user['User'];
        return false;
    }
    
    #given the academic year it gives the graduation year
    function calc_academic_year($academic)
    {
        if ($academic < 0 || $academic > 3)
            throw new Exception('invalid academic year choise');
        $period = 4 - $academic;
        $cur_date = Date('now');
        $cur_year = date("Y");
        $end = Date(($cur_year."/5/1"));
        if ($cur_date > $end)
            $period -= 1;
        return ($cur_year + $period);
    }
    
    function validate_birth_date($data)
    {
        $valid = $this->validate_date($data['birthdate']);
        if ($valid)
        {
            $str = $data['birthdate'];
            $str = explode('-', $str);
            $str = $str[0].$str[1].$str[2];
            if (($timestamp = strtotime($str)) === false)
            {
                return false;
            }
            else
            {
                $birth_dat_stamp = strtotime($str);
                $cur_stamp = strtotime("now");
                if ($cur_stamp - $birth_dat_stamp >= 497664000)
                    return true;
                return false;
            }
        }
    }
}
?>
